window.onload = ()=>{
    var ul = document.querySelector('.operation');
    var addLi = ul.firstElementChild || ul.firstChild;
    EventListener.addEvent(addLi, 'click', (e)=>window.location.href="/officalWebsite/admin/addIntroduction.html");
    // 批量删除
	var delsBtn = ul.children[1];
	EventListener.addEvent(delsBtn, 'click', function(){
		var ids = getCheckedBox();
		if(ids.length>0){
			var bool = confirm('是否确认删除已选中的数据?');
			if(bool){
				ajax({
					method : 'POST',
					url : '/officalWebsite/info/intro?method=removeIntros',
					async : true,
					data : {
						'ids' : ids
					},
					success : function(data){
						if(data==="true"){
							alert('删除成功！');
							searchIntros();
						}else{
							alert('删除失败，请重试！');
						}
					}
				});
			}
		}else{
			alert('请选择要删除的数据！');
		}
	});
    
    function searchIntros(){
    	// 获取表格内容
    	var tbody = document.querySelector('#info');
    	// 移除原有子节点
		clearChilds();
		// 调用异步请求方法
		ajax({
	        method : 'get',
	        url : '/officalWebsite/info/intro?method=findIntros'+'&nowtime='+new Date().getTime(),
	        async : true,
	        success : function(data){
	        	if(data!=="" && data!=null){
	        		var intros = JSON.parse(data);
		        	for(var i=0;i<intros.length;i++){
		        		tbody.innerHTML += '<tr>'+
		        			'<td><input type="checkbox"/></td>'+
		        			'<td>'+(i+1)+'<input type="hidden" value="'+intros[i].introductionId+'" name="introId" /></td>'+
		        			'<td>'+intros[i].introductionTitle+'</td>'+
		        			'<td>'+intros[i].introductionContent+'</td>'+
		        			'<td>'+intros[i].introductionImgPath+'</td>'+
		        			'<td>'+
		        				'<a class="showBtn" href="javascript:;" target="_blank">查看</a>'+
		        			'</td>'+
		        			'</tr>';
		        	}
		        	bindSelectAll();
	        	}
	        },
	    });
    }
    // 初始化调用
    searchIntros();
    
    function bindSelectAll(){
    	var boxs = document.querySelectorAll('input[type="checkbox"]');
    	var selectAll = boxs[0];
    	EventListener.addEvent(selectAll, 'click', function() {
    	    var bool = selectAll.checked;
    	    if (bool) {
    	        for (var i = 1; i < boxs.length; i++) {
    	            boxs[i].checked = bool;
    	        }
    	    } else {
    	        for (var i = 1; i < boxs.length; i++) {
    	            boxs[i].checked = bool;
    	        }    
    	    }
    	});
    }
    
    // 移除tbody的所用子节点
	function clearChilds(){
		var tbody = document.querySelector('#info');
		var trs = tbody.children;	
		// 因HTMLCollection会随着文档的改变而改变，要使用原来的可先将其保存到数组中
		var arr = [];
		for(var i=0;i<trs.length;i++){
			arr.push(trs[i]);
		}
		for(var i=0;i<arr.length;i++){
			tbody.removeChild(arr[i]);
		}
	}
	
}